/**
 * Created by xc on 2016/05/12.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbNotifyUserModel = function(obj){
    ItbBaseModel.call(this,obj);

    // 以下为表定义数据，需要的字段就填入
    //	 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '关联id',
    //   `notify_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '消息id',
    //   `user_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '用户id',
    //   `state` int(3) NOT NULL DEFAULT '0' COMMENT '状态：待定',
    //   `create_time` datetime NOT NULL,
    //   `update_time` datetime NOT NULL,
    //   `update_by` varchar(50) NOT NULL DEFAULT '0',
    //   `del_flg` int(1) NOT NULL DEFAULT '0',

};
ItbNotifyUserModel.prototype = new ItbBaseModel();

// 取得表名
ItbNotifyUserModel.prototype.getTableName = function(){
    return 'itb_notify_user';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbNotifyUserModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
}

// 批量更新状态
// userId: 用户id
// ids: 逗号连接id
// state: 状态
ItbNotifyUserModel.prototype.updState = function(userId, ids, state, callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbNotifyUserModel.updState Err:'+err);
        }
        // Use the connection
        var sql = ' UPDATE itb_notify_user t SET t.update_time=sysdate(), t.update_by='+userId+', t.state = '+state;
        sql += ' WHERE t.del_flg=0 AND t.user_id='+userId+' AND t.id IN ('+ids+')';

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            if(error) {
                console.log('ItbNotifyUserModel.updState Err:'+error);
                callback(error);
            } else {
                callback(null);
            }
        });
    });
};

// 导出
module.exports = ItbNotifyUserModel;